package android.slc.appbase.ui.utils.magicindicator;

import android.content.Context;
import android.slc.appbase.R;
import android.slc.commonlibrary.util.compat.SlcAttrUtils;
import android.slc.commonlibrary.util.compat.SlcColorUtils;

import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.SizeUtils;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.List;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/10/21 下午 12:53
 */
public abstract class TabCommonNavigatorAdapter extends CommonNavigatorAdapter {

    @Override
    public int getCount() {
        return getTabTitles() == null ? 0 : getTabTitles().size();
    }

    @Override
    public IPagerTitleView getTitleView(Context context, final int index) {
        //想要更炫酷可以使用ColorTransitionPagerTitleView，字体颜色会过渡
        SimpleSelectPagerTitleView pagerTitleView = new SimpleSelectPagerTitleView(context);
        pagerTitleView.setTextSize(SizeUtils.px2sp(context.getResources().getDimensionPixelSize(R.dimen.design_tab_text_size)));
        pagerTitleView.setNormalColor(SlcColorUtils.getColor(context, R.color.app_color_on_primary_alpha_6));
        pagerTitleView.setSelectedColor(SlcColorUtils.getColorByAttrResId(context, R.attr.colorOnPrimary));
        pagerTitleView.setText(getTabTitles().get(index));
        pagerTitleView.setBackgroundResource(SlcAttrUtils.getResId(context, R.attr.selectableItemBackground));
        pagerTitleView.setOnClickListener(view -> getViewPager().setCurrentItem(index));
        return pagerTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator indicator = new LinePagerIndicator(context);
        indicator.setLineHeight(SizeUtils.dp2px(2));
        indicator.setColors(SlcColorUtils.getColorByAttrResId(context, R.attr.colorOnPrimary));
        return indicator;
    }

    protected abstract List<String> getTabTitles();

    protected abstract ViewPager2 getViewPager();
}
